#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient

# 连接MongoDB
def InitMongoDB(MONGODB_HOST, MONGODB_PORT, MONGODB_DB_NAME, MONGODB_COLLECTION_NAME):
    client = MongoClient(MONGODB_HOST, MONGODB_PORT)
    db = client[MONGODB_DB_NAME]
    collection = db[MONGODB_COLLECTION_NAME]
    return client, collection


# 关闭MongoDB
def CloseMongoDB(MongoDB_Connection):
    MongoDB_Connection.close()

def ChangeAuthorityFieldType():
    client, collection = InitMongoDB("mongo-node", 27017, "taxonomy", "taxonomy_20180123")

    for result in collection.find({"names_scientific_name": {"$exists": 1}}, {"names_scientific_name": 1}):
        name = result["names_scientific_name"][0]
        id = result["_id"]
        collection.update_one({"_id": id}, {"$set": {"names_scientific_name": name}})

    CloseMongoDB(client)

if __name__ == '__main__':
    ChangeAuthorityFieldType()